package org.nanhill.moo.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nanhill.moo.web.UserSession;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * Custom session check.
 * 
 * @author alin
 * 
 */
public class UserSessionInterceptor extends HandlerInterceptorAdapter {

	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String contextPath = request.getContextPath();
		UserSession userSession = UserSession.getUserSession(request);
		if (userSession == null) {
			String newUrl = contextPath + "/index.og";
			response.sendRedirect(newUrl);
			return false;
		}
		return true;
	}
}